﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStructure
{
    public class NodeList<T>
    {
        #region 字段
        private Node<T> _head;//头结点
        private int _size;//链表的实际长度
        #endregion

        #region 属性
        public Node<T> Head
        {
            get { return _head; }
            set { value = _head; }
        }
        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }
        #endregion

        #region 构造函数
        public NodeList()
        {
            _head = new Node<T>(default(T));
            _size = 0;
        }
        #endregion

        #region 工具方法
        /// <summary>
        /// 向链表中插入一个元素
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Insert(int pos, T data)
        {
            int i = -1;
            Node<T> _current = _head;
            while (_current.Next != null&&i<pos)
            {
                _current = _current.Next;
                i++;
            }
            if (_current.Next == null)
            {
                if (pos == 0)
                {
                    Node<T> tmp = _current.Next;
                    _current.Next = new Node<T>(data,tmp);
                    _size++;
                    return true;
                }
                return false;
            }
            else
            {
                Node<T> tmp = _current.Next;
                _current.Next = new Node<T>(data,tmp);
                _size++;
                return true;
            }
        }
        /// <summary>
        /// 删除链表中的一个元素
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public bool Delete(int pos)
        {
            int i = 0;
            Node<T> node = _head;
            while (node!=null&&i<pos)
            {
                node = node.Next;
                i++;
            }
            if (node == null)
            {
                return false;
            }
            else
            {
                Node<T> next = node.Next;
                node.Next = next.Next;
                _size--;
                return true;
            }
        }
        /// <summary>
        /// 向链表中追加一个元素
        /// </summary>
        /// <param name="data"></param>
        public void Append(T data)
        {
            Node<T> _current=_head;
            while (_current.Next != null)
            {
                _current = _current.Next;
            }
            _current.Next = new Node<T>(data);
            _size++;
        }
        /// <summary>
        /// 判断链表是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return _head.Next == null;
        }
        /// <summary>
        /// 获得链表中指定位置的值
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public Node<T> GetElem(int pos)
        {
            int i = 0;
            Node<T> _current = _head.Next;
            while (_current != null && i < pos)
            {
                _current = _current.Next;
                i++;
            }
            return _current;
        }
        #endregion
    }
}
